set sourceDir [file dir [info script]]
# =====================================
# HWVERSION_2017.1_Apr 12 2017_22:30:32
#ara_weld.tcl
catch {namespace delete ara_weld}

namespace eval ara_weld {
	variable tabname "weldtool"
	variable rstfile "test.rst"
	variable m_Lstep 0
	variable m_Sbstep 0
	variable tplfile
	
	variable nullid 0
	variable width 12
	
	variable system_list
	variable system_name_list
	variable node_num_list
	variable elem_num_list
	variable weld_name_list
	variable weld_num
}
#
proc ::ara_weld::init { } {
	variable tplfile
	set tplfile "$::sourceDir/ara_weld.tpl"
	*templatefileset $tplfile
}

proc ::ara_weld::begin { outfile } { 
	variable rstfile
	variable m_Lstep
	variable m_Sbstep
	puts $outfile "/prep7"
	puts $outfile "\n!!! >>> ------------ set rst file ------------ <<< !!!"
	puts $outfile "*dim,rstfilename,STRING,248"
	puts $outfile "rstfilename(1)='[ file rootname $rstfile ]'"
	if { $m_Lstep == 0 } {
		puts $outfile "Lstep='LAST'"
	} else {
		puts $outfile "Lstep=$m_Lstep"
	}
	if { $m_Sbstep == 0 } {
		puts $outfile "Sbstep='LAST'"
	} else {
		puts $outfile "Sbstep=$m_Sbstep"
	}
}

proc ::ara_weld::end { outfile } {
	variable weld_num
	
	puts $outfile "\n!!!!! .rst files!!!!!"
	puts $outfile "/POST1"  
	puts $outfile "INRES,ALL   "
	puts $outfile "FILE,rstfilename(1),'rst',"
	puts $outfile "SET,Lstep,Sbstep"
	puts $outfile "/Graphics,full    !!!FULL is power off;POWER is power on!!!"
	
	puts $outfile "*do,i,1,$weld_num,1"
	
	puts $outfile "RSYS,Local_id(i)"
	puts $outfile "NodeNumbers=Weld_NodeNumbers(i)"
	puts $outfile "ElementNumbers=Weld_ElemNumbers(i)"
	puts $outfile "ALLSEL,ALL"  
	puts $outfile "EPLOT"
	puts $outfile "ESEL,S,,,%Weld_Name(i)%_ElemList(1),"
	puts $outfile "*do,j,2,ElementNumbers,1"
	puts $outfile "ESEL,A,,,%Weld_Name(i)%_ElemList(j),"
	puts $outfile "*enddo"
	puts $outfile "/Replot"

	puts $outfile "*CFOPEN,Weld_NodalStress_Top,txt,,append"
	puts $outfile "Shell,top "
	puts $outfile "Output_Weld_Name=Weld_Name(i)"
	puts $outfile "Output_Local_Name=Local_Name(i)"
	puts $outfile "*VWRITE,Output_Weld_Name,Output_Local_Name,"
	puts $outfile "(15X,A8,15X,A8)"
	puts $outfile "*VWRITE,"
	puts $outfile "(7X,'NodeID',2X,'NodalStress_XX',2X,'NodalStress_YY',2X,'NodalStress_ZZ',2X,'NodalStress_XY',2X,'NodalStress_YZ',2X,'NodalStress_XZ')"
	puts $outfile "*do,k,1,NodeNumbers,1"
	puts $outfile "NodeID=%Weld_Name(i)%_NodeList(k)"
	puts $outfile "*if,NodeID,NE,0.0,then,"
	puts $outfile "*get,NodalStress_XX,node,NodeID,s,X"
	puts $outfile "*get,NodalStress_YY,node,NodeID,s,Y"
	puts $outfile "*get,NodalStress_ZZ,node,NodeID,s,Z"
	puts $outfile "*get,NodalStress_XY,node,NodeID,s,XY"
	puts $outfile "*get,NodalStress_YZ,node,NodeID,s,YZ"
	puts $outfile "*get,NodalStress_XZ,node,NodeID,s,XZ"
	puts $outfile "*VWRITE,NodeID,NodalStress_XX,NodalStress_YY,NodalStress_ZZ,NodalStress_XY,NodalStress_YZ,NodalStress_XZ,"
	puts $outfile "(F15.0,2X,6F15.2)"
	puts $outfile "*else"
	puts $outfile "*endif"
	puts $outfile "*enddo"
	puts $outfile "*cfclose"

	puts $outfile "*CFOPEN,Weld_NodalStress_Bot,txt,,append"
	puts $outfile "Shell,bot "
	puts $outfile "*VWRITE,Output_Weld_Name,Output_Local_Name,"
	puts $outfile "(15X,A8,15X,A8)"
	puts $outfile "*VWRITE,"
	puts $outfile "(7X,'NodeID',2X,'NodalStress_XX',2X,'NodalStress_YY',2X,'NodalStress_ZZ',2X,'NodalStress_XY',2X,'NodalStress_YZ',2X,'NodalStress_XZ')"
	puts $outfile "*do,k,1,NodeNumbers,1"
	puts $outfile "NodeID=%Weld_Name(i)%_NodeList(k)"
	puts $outfile "*if,NodeID,NE,0.0,then,"
	puts $outfile "*get,NodalStress_XX,node,NodeID,s,X"
	puts $outfile "*get,NodalStress_YY,node,NodeID,s,Y"
	puts $outfile "*get,NodalStress_ZZ,node,NodeID,s,Z"
	puts $outfile "*get,NodalStress_XY,node,NodeID,s,XY"
	puts $outfile "*get,NodalStress_YZ,node,NodeID,s,YZ"
	puts $outfile "*get,NodalStress_XZ,node,NodeID,s,XZ"	
	puts $outfile "*VWRITE,NodeID,NodalStress_XX,NodalStress_YY,NodalStress_ZZ,NodalStress_XY,NodalStress_YZ,NodalStress_XZ,"
	puts $outfile "(F15.0,2X,6F15.2)"
	puts $outfile "*else"
	puts $outfile "*endif"
	puts $outfile "*enddo"
	puts $outfile "*cfclose"
	
	puts $outfile "*enddo"
	puts $outfile "/exit,nosav"
}

proc ::ara_weld::system_out { outfile id } { 
	variable system_list
	variable system_name_list
	set XC [ hm_getvalue systems id=$id dataname=originx ]
	set YC [ hm_getvalue systems id=$id dataname=originy ]
	set ZC [ hm_getvalue systems id=$id dataname=originz ]
	set THXY [ hm_getvalue systems id=$id dataname=thetaxy ]
	set THYZ [ hm_getvalue systems id=$id dataname=thetayz ]
	set THZX [ hm_getvalue systems id=$id dataname=thetazx ]
	set KCN $id
	lappend system_list $KCN
	lappend system_name_list 'LC_$KCN'
	puts $outfile "LOCAL, $KCN, 0, $XC, $YC, $ZC, $THXY, $THYZ, $THZX, , \n"
}

proc ::ara_weld::write_list { outfile name type data num } { 
	puts $outfile "*dim,$name,$type,$num,"
	set i 1
	set n 1
	foreach x $data { 
		if { $i == 1 } {
			puts -nonewline $outfile "$name\($n\)=$x" 
		} elseif { $i == 8 } {
			puts $outfile ",$x"
			set i 0
			set n [ expr $n+8 ]
		} else {
			puts -nonewline $outfile ",$x"
		}
		incr i 
	}
	puts $outfile "\n"
}

#Procedure: ara_weld::GetWeld
proc ::ara_weld::GetWeld { outfile } {
	variable system_list
	variable system_name_list
	variable node_num_list
	variable elem_num_list
	variable weld_name_list
	variable weld_num
	
	*createmarkpanel groups 1 "select welds..."
	set allwelds [ hm_getmark groups 1 ]
	
	set system_list [list]
	set system_name_list [list]
	set node_num_list [list]
	set elem_num_list [list]
	set weld_name_list [list]
	set weld_num 0
	
	set idx 0
	begin $outfile
	puts $outfile "\n!!! >>> ------------ Begin weld information ------------ <<< !!!"
	foreach weld_id $allwelds { 	
		set weld_name [ hm_getvalue groups id=$weld_id dataname=name ]
		set new_weld_name [ string map {. p " " "" "\n" "" "\r" "" "\t" ""} $weld_name ]
		if { [string match {[0-9]*} $new_weld_name ] } {
			set new_weld_name "w$new_weld_name"
		}
		set sys_id [ hm_getvalue groups id=$weld_id dataname=systemid ]
		if { $sys_id == 0 } continue
		
		set eset_id [ hm_getvalue groups id=$weld_id dataname=esetid ]
		if { $eset_id == 0 } continue
		*createmark elements 1 "by set id" $eset_id
		set myelems [ hm_getmark elements 1 ]
		*clearmark elements 1		
		set elem_num [ llength $myelems ]
		if { $elem_num == 0 } continue
		
		set nset_id [ hm_getvalue groups id=$weld_id dataname=nsetid ]
		if { $nset_id == 0 } continue
		*createmark nodes 1 "by set id" $nset_id
		set mynodes [ hm_getmark nodes 1 ]
		*clearmark nodes 1	
		set node_num [ llength $mynodes ]
		if { $node_num == 0 } continue
		
		puts $outfile "!!! --- begin $weld_name --- !!!"
		incr idx
		set elem_set_name [ format "%s%s" $new_weld_name "_ElemList" ]
		set node_set_name [ format "%s%s" $new_weld_name "_NodeList" ]
		system_out $outfile $sys_id
		write_list $outfile $elem_set_name "ARRAY" $myelems $elem_num
		write_list $outfile $node_set_name "ARRAY" $mynodes $node_num
		
		#~ puts $outfile "!!! >>> end $weld_name <<< !!!"
		
		lappend elem_num_list $elem_num
		lappend node_num_list $node_num
		lappend weld_name_list '$new_weld_name'
	}
	
	set weld_num [ llength $weld_name_list ]
	if { $weld_num > 0 } {
		puts $outfile "!!! --- some variables  --- !!!"
		write_list $outfile "Weld_Name" "CHAR" $weld_name_list $weld_num
		write_list $outfile "Local_Name" "CHAR" $system_name_list $weld_num
		write_list $outfile "Local_id" "ARRAY" $system_list $weld_num
		write_list $outfile "Weld_ElemNumbers" "ARRAY" $elem_num_list $weld_num
		write_list $outfile "Weld_NodeNumbers" "ARRAY" $node_num_list $weld_num
	}
	
	puts $outfile "!!! >>> -------- End weld information --------- <<< !!!\n"
	
	end $outfile
}


#=============================================
proc ::ara_weld::write_out { } { 
	set types_r {
		{{rst Files}       {.inp}        }
		{{All Files}        *            }
	}
	set filename [ tk_getSaveFile -defaultextension "inp" -initialdir " " -filetypes $types_r -title "Save File as ..."]
	if { $filename == "" } { return }
	
	#~ if { [ file extension $filename ] == "" } { 
		#~ append filename ".inp"
	#~ }
	
	if [ catch { open "$filename" w} res1 ] {
		puts "Cannot open $filename for write:$res1"
	}
	
	GetWeld $res1
	
	close $res1
	puts   "output : $filename"
}

#=============================================================
proc ::ara_weld::set_rstfile { } {
	variable rstfile
	set types_r {
		{{rst Files}       {.rst}        }
		{{All Files}        *            }
	}
	set newrstfile [ tk_getOpenFile -initialdir " " -filetypes $types_r -title "select result file" ]
	if { $newrstfile == "" } {
	} else {
		set rstfile $newrstfile
	}
}

proc ::ara_weld::set_step { } {
	variable m_Lstep
	set s [ hm_getint "step=" "Please specify the step" ] 
	if { $s >= 0 } {
		set m_Lstep $s
	}
}

proc ::ara_weld::set_substep { } {
	variable m_Sbstep
	set s [ hm_getint "substep=" "Please specify the substep" ]
	if { $s >= 0 } {
		set m_Sbstep $s
	}
}

#=============================================================
proc ::ara_weld::create_weld { } {
	hm_pushpanel "interfaces"
}
#============================================================================
# GUI

proc ::ara_weld::GUIlabel { frm labelname } {
	set frmlable [frame $frm.frm$labelname]
	pack $frmlable -side top -anchor nw 
	set label [label $frmlable.$labelname -text "$labelname"]
	pack $label -side left -anchor nw
}

proc ::ara_weld::GUIvariable { frm varlabel text cmd var } {
	variable width
	set frm_var [frame $frm.frm_$varlabel]
	pack $frm_var -side top -anchor nw 
	set btn   [button $frm_var.btn_$varlabel  -text $text -width $width -command  $cmd ]
	#~ set label [label $frm_var.label_$varlabel -textvariable $var  -width $width]
	set label [label $frm_var.label_$varlabel -textvariable $var ]
	pack $btn $label -side left -anchor nw
}

proc ::ara_weld::GUInullspace { frm } {
	variable nullid
	set null [label $frm.null$nullid -text " "]
	pack $null  -side top -anchor nw
	incr nullid
}

#=============================================================
proc ::ara_weld::ara_weldtool_GUI { mymain } {
	variable rstfile
	variable m_Lstep
	variable m_Sbstep
	variable width
	
	set weldtool [labelframe $mymain.weldtool -text "Weld tools" ]
	pack $weldtool -side top -fill x -anchor nw
	#  set nset  prefix
	::ara_weld::GUIlabel    $weldtool "variables:"
	::ara_weld::GUIvariable $weldtool "setrstfile" "rst file : " ::ara_weld::set_rstfile ::ara_weld::rstfile
	::ara_weld::GUIvariable $weldtool "setstep"    "step : "     ::ara_weld::set_step ::ara_weld::m_Lstep
	::ara_weld::GUIvariable $weldtool "setsubstep" "substep : "  ::ara_weld::set_substep ::ara_weld::m_Sbstep
	
	::ara_weld::GUInullspace $weldtool
	#method:
	::ara_weld::GUIlabel $weldtool "method:"
	
	set frmtool [frame $weldtool.frmtool]
	pack $frmtool -side top -anchor nw 
	set btn_creat     [button $frmtool.btn_creat    -text "Create Weld"  -width $width -command {::ara_weld::create_weld}]
	grid $btn_creat
	
	# out put
	::ara_weld::GUInullspace $weldtool
	::ara_weld::GUIlabel $weldtool "outputs:"
	set frmoutput [frame $weldtool.frmoutput]
	pack $frmoutput -side top -anchor nw 
	set btn_weld [button $frmoutput.btn_weld  -text "Write out"  -width $width -command {::ara_weld::write_out }]
	grid $btn_weld 
}
#=============================================================
proc ::ara_weld::exit_GUI { mymain } {
	variable width
	set frmexit [frame $mymain.frmexit]
	pack $frmexit -side top -fill x -anchor nw	
	set btn_closeSF [button $frmexit.btn_closeSF  -text "exit" -width $width -command {hm_framework removetab $::ara_weld::tabname}] 
	pack $btn_closeSF -side right -anchor nw
}
#=============================================================
proc ::ara_weld::create_GUI {} {
	variable tabname
	# init
	::ara_weld::init
	
	# removetab first
	catch {hm_framework removetab $tabname}	
	catch {	destroy .dtMainFrame }
	
	# main frame
	set dtMainFrame [frame .dtMainFrame]
	pack $dtMainFrame -side top -fill both -expand 1	 
	
	set mymain [frame $dtMainFrame.main]
	pack $mymain -side bottom -fill x -anchor nw
	
	# weld frame
	::ara_weld::ara_weldtool_GUI $mymain
	
	#exit:
	::ara_weld::exit_GUI $mymain
	
	##the following code for hypermesh tab
	hm_framework addtab $tabname $dtMainFrame
}

#==============================================================================================
# main 

::ara_weld::create_GUI
